package io.renren.modules.web.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.web.dao.WebCodeDao;
import io.renren.modules.web.entity.WebCodeEntity;
import io.renren.modules.web.service.WebCodeService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * Web代码服务实现类
 */
@Service
public class WebCodeServiceImpl extends ServiceImpl<WebCodeDao, WebCodeEntity> implements WebCodeService {

    @Autowired
    private WebCodeDao webCodeDao;

    @Override
    public List<WebCodeEntity> getCodesByUserId(Integer userId) {
        return webCodeDao.getCodesByUserId(userId);
    }

    @Override
    public List<WebCodeEntity> getPublicCodes() {
        return webCodeDao.getPublicCodes();
    }

    @Override
    public List<WebCodeEntity> getCodesByType(Integer codeType) {
        return webCodeDao.getCodesByType(codeType);
    }

    @Override
    public String runCode(String htmlCode, String cssCode, String jsCode) {
        // 构建完整的HTML文档
        StringBuilder fullHtml = new StringBuilder();
        fullHtml.append("<!DOCTYPE html>\n");
        fullHtml.append("<html lang=\"zh-CN\">\n");
        fullHtml.append("<head>\n");
        fullHtml.append("<meta charset=\"UTF-8\">\n");
        fullHtml.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n");
        fullHtml.append("<title>Web代码运行结果</title>\n");
        
        // 添加CSS样式
        if (cssCode != null && !cssCode.trim().isEmpty()) {
            fullHtml.append("<style>\n");
            fullHtml.append(cssCode);
            fullHtml.append("\n</style>\n");
        }
        
        fullHtml.append("</head>\n");
        fullHtml.append("<body>\n");
        
        // 添加HTML内容
        if (htmlCode != null && !htmlCode.trim().isEmpty()) {
            fullHtml.append(htmlCode);
        }
        
        // 添加JavaScript代码
        if (jsCode != null && !jsCode.trim().isEmpty()) {
            fullHtml.append("<script>\n");
            fullHtml.append(jsCode);
            fullHtml.append("\n</script>\n");
        }
        
        fullHtml.append("</body>\n");
        fullHtml.append("</html>");
        
        return fullHtml.toString();
    }

    @Override
    public boolean saveCode(WebCodeEntity webCodeEntity) {
        // 设置创建时间和更新时间
        String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        if (webCodeEntity.getCreateTime() == null) {
            webCodeEntity.setCreateTime(currentTime);
        }
        webCodeEntity.setUpdateTime(currentTime);
        Long userId = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUserId();
        webCodeEntity.setUserId(userId.intValue());
        
        return save(webCodeEntity);
    }
} 